package com.info33.platform.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.validated.SaveValidated;
import com.info33.platform.common.validated.UpdateValidated;
import com.info33.platform.system.dto.SysTenantDTO;
import com.info33.platform.system.entity.SysTenant;
import com.info33.platform.system.service.SysTenantService;
import com.info33.platform.system.vo.SysTenantVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author admin
 */
@RestController
@RequestMapping("/SysTenant")
@Api(tags = "系统 - 租户")
public class SysTenantController extends BaseController<SysTenantService, SysTenant> {

    private final SysTenantService sysTenantService;

    @Autowired
    public SysTenantController(SysTenantService sysTenantService) {
        super(sysTenantService);
        this.sysTenantService = sysTenantService;
    }

    @PostMapping("/saveTenant")
    @ApiOperation(value = "新增单个租户信息")
    public Result<?> saveTenant(@Validated(value = {SysTenantDTO.SaveTenant.class, SaveValidated.class}) @RequestBody SysTenantDTO sysTenantDTO) {
        return sysTenantService.saveTenant(sysTenantDTO);
    }

    @PostMapping("/updateTenant")
    @ApiOperation(value = "更新单个租户信息")
    public Result<?> updateTenant(@Validated(value = {SysTenantDTO.UpdateTenant.class, UpdateValidated.class}) @RequestBody SysTenantDTO sysTenantDTO) {
        return sysTenantService.updateTenant(sysTenantDTO);
    }

    @PostMapping(value = "/pageTenantList/{pageNum}/{pageSize}")
    @ApiOperation(value = "分页获取系统租户列表", notes = "分页获取系统租户列表")
    public Result<IPage<SysTenantVO>> pageTenantList(@RequestBody SysTenant sysTenant, @PathVariable int pageNum, @PathVariable int pageSize) {
        IPage<SysTenantVO> page = new Page<SysTenantVO>(pageNum, pageSize);
        return Result.ok(sysTenantService.pageTenantList(page, sysTenant));
    }

    @GetMapping(value = "/getDetailById/{id}")
    @ApiOperation(value = "根据id获取租户详情+用户信息+菜单树+下发菜单列表")
    public Result<SysTenantVO> getDetailById(@PathVariable Long id) {
        return Result.ok(sysTenantService.getDetailById(id, getTenantId()));
    }

}